package com.catersystem.zmy.catersystem.fragment;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.SwitchCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.catersystem.zmy.catersystem.R;
import com.catersystem.zmy.catersystem.base.BaseFragment;
import com.catersystem.zmy.catersystem.utils.DialogUtil;
import com.catersystem.zmy.catersystem.utils.KeyboardUtils;
import com.catersystem.zmy.catersystem.utils.SPUtils;
import com.catersystem.zmy.catersystem.utils.ScreenUtil;
import com.catersystem.zmy.catersystem.utils.SoundPlayUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 操作模式
 * Created by Administrator on 2017/10/30 0030.
 */

public class OperateFragment extends BaseFragment {

    @BindView(R.id.tv_overtime)
    TextView mTvOvertime;//超时预警时间
    @BindView(R.id.sc_voice)
    SwitchCompat mScVoice;
    @BindView(R.id.tv_switch_status)
    TextView mTvSwitchStatus;

    private Dialog mDialogOvertime;//超时时间dialog
    private View mOvertimeView;//超时预警弹窗布局
    private RadioGroup mRgTime;//超时时间选择
    private TextView mTvSave;//保存
    private TextView mTvCancel;//取消
    private ImageView mIvCancel1;//关闭
    private String mOvertime;//超时时间

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_setting_operate, container, false);
    }

    @Override
    protected void initView(View view) {
        mScVoice.setChecked(SPUtils.getInstance().getBoolean("voice_switch"));
        mTvSwitchStatus.setText(SPUtils.getInstance().getBoolean("voice_switch") ? "开" : "关");
        mScVoice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SPUtils.getInstance().put("voice_switch", isChecked);
                if (isChecked) {
                    SoundPlayUtils.init(activity).play();
                } else {
                    SoundPlayUtils.init(activity).stop();
                }
            }
        });
        findDialogOvertime();
        if (mDialogOvertime == null) {
            mDialogOvertime = DialogUtil.getDialog1(activity, true, mOvertimeView, ScreenUtil.getScreenWidthPix
                    (activity)
                    * 0.5, ScreenUtil.getScreenHeightPix(activity) * 0.4);
        }

        //默认5分钟超时预警
        mTvOvertime.setText(SPUtils.getInstance().getString("overtime") == "" ? "5" : SPUtils.getInstance().getString
                ("overtime"));
    }


    /**
     * 菜品超时预警控件实例化
     */
    private void findDialogOvertime() {
        mOvertimeView = View.inflate(activity, R.layout.dialog_dishes_overtime, null);
        mRgTime = (RadioGroup) mOvertimeView.findViewById(R.id.rg_time);
        mIvCancel1 = (ImageView) mOvertimeView.findViewById(R.id.iv_cancel);
        mTvSave = (TextView) mOvertimeView.findViewById(R.id.tv_save);
        mTvCancel = (TextView) mOvertimeView.findViewById(R.id.tv_cancel);
        mTvSave.setOnClickListener(mOnClickListener);
        mTvCancel.setOnClickListener(mOnClickListener);
        mIvCancel1.setOnClickListener(mOnClickListener);

        //分钟选择
        mRgTime.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_one://1分钟
                        mOvertime = "1";
                        break;
                    case R.id.rb_three://3分钟
                        mOvertime = "3";
                        break;
                    case R.id.rb_five://5分钟
                        mOvertime = "5";
                        break;
                    case R.id.rb_ten://10分钟
                        mOvertime = "10";
                        break;
                    case R.id.rb_fifteen://15分钟
                        mOvertime = "15";
                        break;
                    case R.id.rb_twenty://20分钟
                        mOvertime = "20";
                        break;
                }

            }
        });
    }

    /**
     * 菜品超时时间弹窗点击事件
     */
    View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_save://保存
                    if (TextUtils.isEmpty(mOvertime)) {
                        showShortToast("请选择菜品超时预警时间！");
                        return;
                    }
                    mTvOvertime.setText(mOvertime);
                    mDialogOvertime.dismiss();
                    SPUtils.getInstance().put("overtime", mOvertime);
                    break;
                case R.id.tv_cancel://取消
                case R.id.iv_cancel:
                    mDialogOvertime.dismiss();
                    break;
            }
        }
    };


    @OnClick({R.id.tv_overtime, R.id.tv_clear_cache})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_overtime://超时时间
                mDialogOvertime.show();
                //取消选中
                mRgTime.clearCheck();
                break;
            case R.id.tv_clear_cache://清除缓存
                DialogUtil.getAlertDialog(activity, "清除本地缓存", "是否清除本地所有缓存? ", "确定", "取消", new DialogInterface
                        .OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SPUtils.getInstance().clear();
                    }
                }).show();
                break;
        }
    }
}
